const Bmob = require('../../utils/bmob')
const utils = require('../../utils/utils')
Page({
  data: {
    payload: [],
    pageIndex: 0
  },
  onLoad() {
    this.loadData()
  },
  loadData() {
    let query = new Bmob.Query('Ix')
    query.descending('createdAt');
    query.skip(this.data.pageIndex * utils.pageSize)
    query.limit(utils.pageSize)
    query.find().then(results => {
      wx.stopPullDownRefresh();
      results.map(item => {
        item.set('btc_amount', (item.get('btc_yesterday') / item.get('btc_rate')).toFixed())
        item.set('ix_btc', (item.get('ix_rate') / item.get('btc_rate')).toFixed(2))
        item.set('createdAt', utils.todayDateTime(item.createdAt))
        return item
      })
      results = utils.formatResults(results)

      let payload = this.data.payload
      if (this.data.pageIndex == 0) {
        payload = []
      }
      payload = payload.concat(results)
      this.setData({
        payload: payload,
        loadingTip: results.length < utils.pageSize ? '已经没有更多啦' : '上拉加载更多'
      })
    })
  },
  onPullDownRefresh: function () {
    // 下拉刷新
    this.setData({
      pageIndex: 0
    })
    this.loadData();
  },
  onReachBottom() {
    let pageIndex = this.data.pageIndex
    pageIndex++
    this.setData({
      pageIndex: pageIndex
    })
    this.loadData()
  }
})